package com.itboye.ihomebank.myzhujia.qianbao;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.BindCardsList;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.me.ActivityZhangHu;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.CashierInputFilter;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 提现
 * 
 */
public class ActivityTiXian extends BaseOtherActivity implements Observer {
	TextView add_shap_title_tv;
	ImageView close_icon;

	RelativeLayout tixian_rl;

	RoundImageView tixian_img;
	TextView tixian_yinhang, tixian_weihao;
	EditText tixian_money;
	TextView tixian_ok;

	private Dialog dialog;// 自定义Dialog
	UserPresenter userPresenter;
	private String uid;
	private ArrayList<BindCardsList> arrayList = new ArrayList<BindCardsList>();
	private String cardNo;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.tixian_rl:
			if (arrayList.size() > 0) {
				Intent intent = new Intent(this, ActivityZhangHu.class);
				intent.putExtra("type", 1);
				startActivityForResult(intent, 101);
			} else {
				ByAlert.alert("没有绑定银行卡,请先返回去绑定银行卡哦！");
			}
			break;
		case R.id.tixian_ok:// 完成
			String money = tixian_money.getText().toString();
			if (money.equals("")) {
				ByAlert.alert("请输入有效金额");
				return;
			}
			DecimalFormat df = new DecimalFormat("#.00");// 保留两位小数
			BigDecimal bg = new BigDecimal(Double.parseDouble(money) * 100);
			String innermoney = df.format(bg);
			userPresenter.tixian(uid, innermoney, cardNo);

			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_ti_xian;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 102) {
			BindCardsList bindCardsList = (BindCardsList) arg2
					.getSerializableExtra("model");
			cardNo = bindCardsList.getId();
			tixian_yinhang.setText(bindCardsList.getBank().get(0));
			tixian_weihao.setText(bindCardsList.getBank().get(2));
			tixian_weihao.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onInit() {
		add_shap_title_tv.setText("提现");

		InputFilter[] filter = { new CashierInputFilter() };
		tixian_money.setFilters(filter);// 设置输入为金额
		userPresenter = new UserPresenter(this);
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		userPresenter.bankList(uid);
		

	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	/**
	 * 自定义Dialog
	 */
	private void showExtraDialog(String msg) {
		dialog = new Dialog(ActivityTiXian.this, R.style.rel_dialog);
		View view = LayoutInflater.from(ActivityTiXian.this).inflate(
				R.layout.user_dialog, null);
		dialog.setContentView(view);
		LinearLayout dialog_ok = (LinearLayout) view
				.findViewById(R.id.dialog_ok);
		TextView dialog_text = (TextView) view.findViewById(R.id.dialog_text);
		dialog_text.setText(msg);
		dialog.setCanceledOnTouchOutside(false);
		dialog_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				dialog.dismiss();
			}
		});
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		// 屏幕宽度
		int screenWidth = display.getWidth();
		// 屏幕高度
		int screenHeight = display.getHeight();
		Window window = dialog.getWindow();

		WindowManager.LayoutParams lp = window.getAttributes();
		window.setGravity(Gravity.CENTER);
		lp.width = screenWidth * 3 / 4;
		lp.height = LayoutParams.WRAP_CONTENT;
		window.setAttributes(lp);

	}
	

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.bindCardList_success) {
				arrayList = (ArrayList<BindCardsList>) entity.getData();
				if (arrayList != null) {
					if (arrayList.size() > 0) {
						cardNo = arrayList.get(0).getId();
						tixian_yinhang.setText(arrayList.get(0).getBank()
								.get(0));
						tixian_weihao
								.setText(arrayList.get(0).getBank().get(2));
						tixian_weihao.setVisibility(View.VISIBLE);
					} else {
						tixian_yinhang.setText("没有绑定银行卡");
						tixian_weihao.setVisibility(View.GONE);
					}
				}
			} else if (entity.getEventType() == UserPresenter.bindCardList_fail) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.tixian_success) {
				showExtraDialog(entity.getData().toString());
			} else if (entity.getEventType() == UserPresenter.tixian_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}
}
